package com.woniuxy.credit_loans.product_liu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.credit_loans.accounting_shi.service.AccountService;
import com.woniuxy.credit_loans.accounting_shi.service.BankService;
import com.woniuxy.credit_loans.entity.*;
import com.woniuxy.credit_loans.product_liu.dto.ProductAppDto;
import com.woniuxy.credit_loans.product_liu.dto.ProductRecordDto;
import com.woniuxy.credit_loans.product_liu.service.ProductAppService;
import com.woniuxy.credit_loans.product_liu.service.ProductRecordService;
import com.woniuxy.credit_loans.product_liu.service.ProductService;
import com.woniuxy.credit_loans.product_liu.vo.ProductAppVo;
import com.woniuxy.credit_loans.product_liu.vo.ProductVo;
import com.woniuxy.credit_loans.staff_ma.service.ChangeRoleService;
import com.woniuxy.credit_loans.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Api(tags = "产品申请表的相关接口")
@RestController
@RequestMapping("/productApps")
public class ProductAppController {
    @Autowired
    private ProductAppService productAppService;
    @Autowired
    private ProductService productService;
    @Autowired
    private ProductRecordService productRecordService;
    @Autowired
    private ChangeRoleService changeRoleService;
    @Autowired
    private AccountService accountService;
    @Autowired
    private BankService bankService;

    @ApiOperation(value = "查询所有状态为待审核及申请下架的产品申请信息")
    @PostMapping("/AllProductApp")
    public Result selectAllProductApp(@RequestBody ProductAppDto productAppDto){
        PageHelper.startPage(productAppDto.getCurrentPage(),productAppDto.getPageSize(),true);
        List<ProductVo> products = productService.selectWaittingProduct();
        PageInfo<ProductAppVo> pageInfo = new PageInfo(products);
        return Result.ok().data("productAppList",products).data("pageInfo",pageInfo.getTotal());
    }

    @ApiOperation(value = "点击提交申请后，数据插入到产品申请表中")
    @PostMapping("")
    public Result insertProductApp(@RequestBody ProductAppDto productAppDto){
        ChangeRole changeRole = changeRoleService.selectByPrimaryKey(productAppDto.getCompanyId());
        BankTable bankTable = bankService.selectByBankName(productAppDto.getBankUserId());
//        if ((bankTable.getBankId().toString() != changeRole.getBankId()) || (productAppDto.getProductappBy1() !=
//                changeRole.getOwnerAccount())){
//
//            ChangeRole changeRole1 = new ChangeRole();
//            changeRole1.setCompanyName(changeRole.getCompanyName());
//            changeRole1.setBankId(bankTable.getBankId().toString());
//            changeRole1.setOwnerAccount(productAppDto.getProductappBy1());
//            changeRoleService.insertSelective(changeRole1);
//
//            Account account = new Account();
//            account.setAccountNo(productAppDto.getProductappBy1());
//            account.setAccountName(changeRole.getCompanyName());
//            account.setAccountBank(bankTable.getBankName());
//            account.setAccountType("合作公司");
//            account.setAccountRevenue(new BigDecimal(0));
//            account.setAccountOutlay(new BigDecimal(0));
//            account.setAccountBanlance(productAppDto.getProductappRegisteredCapital());
//            accountService.insertSelective(account);
//        }

        Product product = new Product();
        product.setProductName(productAppDto.getProductappName());
        product.setProductDesc(productAppDto.getProductappDesc());
        product.setProductPicture(productAppDto.getProductappRegisteredCapital().toString());
        product.setProductFlag(1);
        product.setProductLabel(1);
        product.setProductAmount(new BigDecimal(productAppDto.getProductappPicture()));
        product.setProductTypeId(productAppDto.getProductTypeId());
        product.setProductPaymethod(Integer.parseInt(productAppDto.getProductappBy2()));
        product.setProductBy1(productAppDto.getCompanyId().toString());
        product.setProductBy2(bankTable.getBankId().toString());
        int i = productService.insertSelective(product);

        productAppDto.setBankUserId(bankTable.getBankId().toString());
        productAppDto.setProductappFlag(1);
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        productAppDto.setProductappDate(format.format(new Date()));
        int i1 = productAppService.insertByBankName(productAppDto);
        if(i1 > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }

    @ApiImplicitParam(name = "productId",value = "产品表的序号",paramType = "path",required = true)
    @GetMapping("{id}")
    public Result selectProduct(@PathVariable int id){
        Product product = productService.selectByPrimaryKey(id);
        return Result.ok().data("product",product);
    }

    @ApiOperation(value = "产品经理点击表单提交，后台根据选取的是否同意来进行插入产品和修改产品状态操作")
    @PostMapping("/insertProduct")
    public Result insertProduct(@Valid @RequestBody ProductRecordDto productDto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return Result.error(bindingResult.getFieldError().getDefaultMessage());
        }

        int i = productRecordService.insertProductRecord(productDto);

        Product product1 = productService.selectByPrimaryKey(productDto.getProductId());
        Product product = new Product();
        if (product1.getProductFlag()==1){
            if (productDto.getProductrecordResult()==1){
                product.setProductId(productDto.getProductId());
                product.setProductFlag(2);
            }else if(productDto.getProductrecordResult()==2){
                product.setProductId(productDto.getProductId());
                product.setProductFlag(3);
            }
        }else if (product1.getProductFlag()==4){
            if (productDto.getProductrecordResult()==1){
                product.setProductId(productDto.getProductId());
                product.setProductFlag(5);
            }else if(productDto.getProductrecordResult()==2){
                product.setProductId(productDto.getProductId());
                product.setProductFlag(3);
        }
        }
        int i1 = productService.updateByPrimaryKeySelective(product);
        if(i1 > 0){
            return Result.ok();
        }else{
            return Result.error();
        }
    }
}